﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace CRM.BLL
{
    public class SysBase
    {
        /// <summary>
        /// 高效分页获取数据
        /// </summary>
        public static DataTable GetDataList(Model.DataTogther DataModel, out int Totail)
        {
            DataTable DTdata = null;
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@TableName", SqlDbType.VarChar,200),
					new SqlParameter("@FieldList", SqlDbType.VarChar,2000),
					new SqlParameter("@PrimaryKey", SqlDbType.VarChar,100),
					new SqlParameter("@Where", SqlDbType.VarChar,1000),
					new SqlParameter("@Order", SqlDbType.VarChar,100),
					new SqlParameter("@SortType", SqlDbType.Int),
					new SqlParameter("@RecorderCount", SqlDbType.Int),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@TotalCount", SqlDbType.Int)};
                strPar[0].Value = DataModel.TableName;
                strPar[1].Value = DataModel.FieldList;
                strPar[2].Value = DataModel.PrimaryKey;
                strPar[3].Value = DataModel.Where;
                strPar[4].Value = DataModel.Order;
                strPar[5].Value = DataModel.SortType;
                strPar[6].Value = DataModel.RecorderCount;
                strPar[7].Value = DataModel.PageSize;
                strPar[8].Value = DataModel.PageIndex;
                strPar[9].Direction = ParameterDirection.Output;

                DTdata = DBUtility.SqlHelper.ExecuteTable(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "DataPageList", strPar);
                Totail = Convert.ToInt32(strPar[9].Value);
                return DTdata;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 从远程服务器同步数据
        /// </summary>
        public static DataTable DownData(Model.DataTogther DataModel, string Constr)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@TableName", SqlDbType.VarChar,200),
					new SqlParameter("@FieldList", SqlDbType.VarChar,2000),
					new SqlParameter("@Where", SqlDbType.VarChar,1000),
					new SqlParameter("@Order", SqlDbType.VarChar,100),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@MaxPage", SqlDbType.SmallInt),
					new SqlParameter("@TotalCount", SqlDbType.Int),
					new SqlParameter("@Descript", SqlDbType.VarChar,100)};
                strPar[0].Value = DataModel.TableName;
                strPar[1].Value = DataModel.FieldList;
                strPar[2].Value = DataModel.Where;
                strPar[3].Value = DataModel.Order;
                strPar[4].Value = DataModel.PageSize;
                strPar[5].Value = DataModel.PageIndex;
                strPar[6].Direction = ParameterDirection.Output;
                strPar[7].Direction = ParameterDirection.Output;
                strPar[8].Direction = ParameterDirection.Output;

                return DBUtility.SqlHelper.ExecuteTable(Constr, CommandType.StoredProcedure, "DataToghter", strPar);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 返回会员数据总数
        /// </summary>
        /// <returns></returns>
        public static int GetUserCount()
        {
            try
            {
                return Convert.ToInt32(DBUtility.SqlHelper.ExecuteScalar(DBUtility.SqlHelper.sqlConnectionStringService, CommandType.StoredProcedure, "PRO_GetUserCount", null));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 返回企业数据总数
        /// </summary>
        /// <returns></returns>
        public static int GetComCount()
        {
            try
            {
                return Convert.ToInt32(DBUtility.SqlHelper.ExecuteScalar(DBUtility.SqlHelper.sqlConnectionStringZYQY, CommandType.StoredProcedure, "PRO_GetCpanyCount", null));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 编辑客户信息[添加/编辑]
        /// </summary>
        /// <param name="DataModel"></param>
        /// <returns></returns>
        public static bool AddNewsData(Model.DataUserInfo DataModel, out string NewUserID)
        {
            bool strRe = false;
            SqlParameter[] strPar = {
					new SqlParameter("@T_Person", SqlDbType.NChar,50),
					new SqlParameter("@T_Sex", SqlDbType.NChar,50),
					new SqlParameter("@T_Tel", SqlDbType.NChar,50),
					new SqlParameter("@T_Fax", SqlDbType.NChar,50),
					new SqlParameter("@T_Mobile", SqlDbType.NChar,100),
					new SqlParameter("@T_Email", SqlDbType.NChar,50),
					new SqlParameter("@T_Province", SqlDbType.NChar,10),
					new SqlParameter("@T_City", SqlDbType.NChar,10),
					new SqlParameter("@T_CompanyName", SqlDbType.NChar,50),
					new SqlParameter("@T_Address", SqlDbType.NChar,200),
					new SqlParameter("@T_PostCode", SqlDbType.NChar,50),
					new SqlParameter("@T_QQ", SqlDbType.NChar,50),
					new SqlParameter("@T_WebUrl", SqlDbType.NChar,50),
					new SqlParameter("@T_UserType", SqlDbType.TinyInt,1),
					new SqlParameter("@T_UserLeave", SqlDbType.TinyInt,1),
					new SqlParameter("@T_UserFrom", SqlDbType.TinyInt,1),
					new SqlParameter("@T_UserState", SqlDbType.TinyInt,1),
					new SqlParameter("@T_DateTime", SqlDbType.DateTime),
					new SqlParameter("@T_Remark", SqlDbType.NChar,500),
					new SqlParameter("@T_UserID", SqlDbType.Int),
					new SqlParameter("@T_NowService", SqlDbType.NChar,10),
					new SqlParameter("@T_User", SqlDbType.VarChar,10)};

            strPar[0].Value = SubStringDATA(DataModel.T_Person, 50);
            strPar[1].Value = SubStringDATA(DataModel.T_Sex, 10);
            strPar[2].Value = SubStringDATA(DataModel.T_Tel, 300);
            strPar[3].Value = SubStringDATA(DataModel.T_Fax, 300);
            strPar[4].Value = SubStringDATA(DataModel.T_Mobile, 300);
            strPar[5].Value = SubStringDATA(DataModel.T_Email, 50);
            strPar[6].Value = SubStringDATA(DataModel.T_Province, 10);
            strPar[7].Value = SubStringDATA(DataModel.T_City, 10);
            strPar[8].Value = SubStringDATA(DataModel.T_CompanyName, 100);
            strPar[9].Value = SubStringDATA(DataModel.T_Address, 200);
            strPar[10].Value = SubStringDATA(DataModel.T_PostCode, 10);
            strPar[11].Value = SubStringDATA(DataModel.T_QQ, 50);
            strPar[12].Value = SubStringDATA(DataModel.T_WebUrl, 50);
            strPar[13].Value = DataModel.T_UserType;
            strPar[14].Value = DataModel.T_UserLeave;
            strPar[15].Value = DataModel.T_UserFrom;
            strPar[16].Value = DataModel.T_UserState;
            strPar[17].Value = DateTime.Now;
            strPar[18].Value = SubStringDATA(DataModel.T_Remark, 50);
            strPar[19].Value = DataModel.T_UserID;
            strPar[20].Value = DataModel.T_NowService;
            strPar[21].Direction = ParameterDirection.Output;

            strRe = (DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "EditNewUserInfo", strPar) > 0) ? true : false;
            NewUserID = strPar[21].Value.ToString();
            return strRe;
        }

        private static string SubStringDATA(string oldStr, int Len)
        {
            if (string.IsNullOrEmpty(oldStr))
            {
                return "";
            }
            else
            {
                return (oldStr.Length > Len) ? oldStr.Substring(0, Len) : oldStr;
            }
        }

        public static void WriteFile(string Path, string Strings)
        {
            if (!System.IO.File.Exists(Path))
            {
                System.IO.FileStream f = System.IO.File.Create(Path);
                f.Close();
            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, false, System.Text.Encoding.GetEncoding("gb2312"));
            f2.WriteLine(Strings);
            f2.Close();
            f2.Dispose();
        }
    }

}
